#include "TreeModel.h"
#include "QStandardItem"
#include<QDebug>

bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    //TreeModel::setData 是重写的父类函数，是在编辑完后会被自动调用的函数
    //TreeItem::setData 是我自己写的函数，来真正修改数据
    if (index.isValid() && role == Qt::EditRole)
    {
        QStandardItem *item = static_cast<QStandardItem*>(index.internalPointer());
        QStandardItem *child = item->child(index.row(),0);
        QStandardItem *it = item->child(index.row(),index.column());
        int OLindex = locateVertex(g,child->text().toStdString());
        int SLindex = SLsearch(list,child->text().toStdString());
        if(index.column()==0){
            g.xList[OLindex].data = value.toString().toStdString();
            list.elem[SLindex].name = value.toString().toStdString();
        }
        else if(index.column()==1){
            list.elem[SLindex].hours = value.toInt();
        }
        else if(index.column()==2){
            list.elem[SLindex].difficulty = value.toInt();
        }
        it->setText(value.toString());
        emit dataChanged(index, index);
        return true;
    }
    return false;
}
